package com.kye.express.business.order_list;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatActivity;
import com.kye.express.business.login.LoginActivity;
import com.kye.express.business.order.NameCertificationUtils;
import com.kye.express.business.order.add_order.AddOrderActivity;
import com.kye.express.business.order.scan_code_add_order.ScanCodeAddOrderActivity;
import com.kye.express.business.scanner.WaybillScannerActivity;
import com.kye.express.databinding.ActivityOrderListBinding;
import com.kye.express.view.KyeProgressDialog;
import com.kye.model.bean.order.OrderStatusType;
import com.kye.model.bean.order.OrderValidationNameCertificationBean;
import com.kye.model.bean.order.scan_code_order.ScanCodeOrderAddress;

import io.reactivex.functions.Consumer;

public class OrderListActivity extends BaseAppCompatActivity implements OrderListContract.OrderListView {

    private final static String TAG_ORDER_STATUS_TYPE = "TAG_ORDER_STATUS_TYPE";
    protected ActivityOrderListBinding mDataBinding;
    protected OrderListContract.OrderListPresenter mPresenter;
    protected OrderListPageAdapter mPageAdapter;
    private String[] titlesTake = {
            "待取货",
            "已取货",
    };
    private OrderStatusType orderStatusType;
    private Dialog mDialog;
    private Dialog mTipsDialog;

    private Dialog mProgressDialog;

    /**
     * 待取货，已取货
     *
     * @param context
     * @return
     */
    public static Intent newIntent(Context context, OrderStatusType orderStatusType) {
        Intent intent = new Intent(context, OrderListActivity.class);
        intent.putExtra(TAG_ORDER_STATUS_TYPE, orderStatusType);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            orderStatusType = (OrderStatusType) getIntent().getSerializableExtra(TAG_ORDER_STATUS_TYPE);
        } else {
            orderStatusType = (OrderStatusType) savedInstanceState.getSerializable(TAG_ORDER_STATUS_TYPE);
        }
        mDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_order_list);
        mPresenter = new OrderListContract.OrderListPresenter();
        mPresenter.bind(this);
        mPresenter.onCreate(savedInstanceState);
        initToolbar(savedInstanceState);
        initViews(savedInstanceState);
        initEvents(savedInstanceState);
        initData(savedInstanceState);
    }

    private void initToolbar(Bundle savedInstanceState) {
        RxView.clicks(mDataBinding.back)
                .compose(this.bindToLifecycleDestroy())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        finish();
                    }
                });
    }

    private void initViews(Bundle savedInstanceState) {
        mPageAdapter = new OrderListPageAdapter(getSupportFragmentManager());
        mDataBinding.orderPages.setOffscreenPageLimit(2);
        mDataBinding.orderPages.setAdapter(mPageAdapter);
        mDataBinding.tabLayout.setViewPager(mDataBinding.orderPages, titlesTake);
        switch (orderStatusType) {
            case ORDER_TO_TAKE:
                mDataBinding.orderPages.setCurrentItem(0);
                break;
            case ORDER_TAKEN:
                mDataBinding.orderPages.setCurrentItem(1);
                break;
            default:
                throw new IllegalStateException("orderStatusType error: " + orderStatusType);
        }
    }

    private void initEvents(Bundle savedInstanceState) {
        RxView.clicks(mDataBinding.buttonAddOrder)
                .compose(new DebounceObservableTransformer<>())
                .compose(bindToLifecycle())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        NameCertificationUtils.checkStartActivity(OrderListActivity.this, mDialog, new NameCertificationUtils.CheckListener() {
                            @Override
                            public void onCreateDialog(Dialog dialog) {
                                mDialog = dialog;
                            }

                            @Override
                            public void startLoginActivity() {
                                LoginActivity.startActivity(OrderListActivity.this);
                            }

                            @Override
                            public void checkNameCertification(Type type) {
                                mPresenter.checkNameCertification(type);
                            }

                            @Override
                            public void checkCustomCodeForStartScanCodeAddOrder(String customCode) {
                                mPresenter.checkCustomCodeForStartScanCodeAddOrder(customCode);
                            }
                        });
                    }
                });
    }

    private void initData(Bundle savedInstanceState) {

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putSerializable(TAG_ORDER_STATUS_TYPE, orderStatusType);
        mPresenter.onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDialog != null && mDialog.isShowing()) mDialog.dismiss();
    }

    @Override
    public void onError() {

    }

    @Override
    public void onError(String msg) {

    }

    @Override
    public void onLoading() {
        mProgressDialog = KyeProgressDialog.show(this, mProgressDialog);
    }

    @Override
    public void onLoaded() {
        KyeProgressDialog.dismiss(mProgressDialog);
    }

    @Override
    public void onNeedValidation(OrderValidationNameCertificationBean bean, NameCertificationUtils.CheckListener.Type type) {
        if (bean.isValidation()) {
            mTipsDialog = NameCertificationUtils.getValidationCertification(this, mTipsDialog, bean.getMessage(), type);
            mTipsDialog.show();
        } else {
            if (type == NameCertificationUtils.CheckListener.Type.ADD_ORDER) {
                AddOrderActivity.startActivity(this);
            } else {
                WaybillScannerActivity.startActivity(this, WaybillScannerActivity.Type.SCAN_ADD_ORDER);
            }
        }
    }

    @Override
    public void onCanScanCodeAddOrder(String customerCode, ScanCodeOrderAddress addressData) {
        ScanCodeAddOrderActivity.startActivity(this, customerCode, addressData);
        finish();
    }

    @Override
    public void onCanNotScanCodeAddOrder() {
        onError("你没有权限扫码下单");
    }
}
